//
//  FreeViewController.m
//  HaoHaiSpell
//
//  Created by haohai on 16/6/17.
//  Copyright © 2016年 海城. All rights reserved.
//

#import "FreeViewController.h"
#import "FirstClassificationCell.h"
#import "SecondClassificationCell.h"
#import "FourthSectionCell.h"
#import "SearchCell.h"
@interface FreeViewController ()<UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong)UICollectionView *spellCollectionView;//集合视图
@property (nonatomic,strong)NSMutableArray *allDataSource;//数据源
@property (nonatomic,strong)NSMutableDictionary *parameter;//参数
@property (nonatomic,strong)UISearchBar *contentSearchBar;//搜索框
@property (nonatomic,strong)NSMutableArray *titleArr;
@property (nonatomic,strong)NSMutableArray *secondTitleArr;
@property (nonatomic,strong)NSMutableArray *cellArr;
@property (nonatomic,strong)NSArray *id_cellArray;//cell标志符数组
<<<<<<< HEAD
@property (nonatomic,strong)UILabel *selLabel;
@property (nonatomic,strong)UILabel *selectedLabel;

=======
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
@end

@implementation FreeViewController
#pragma mark － 懒加载
-(NSMutableArray *)allDataSource{
    
    if (!_allDataSource) {
        _allDataSource=[NSMutableArray new];
    }
    return _allDataSource;
}

-(NSMutableDictionary *)parameter{
    
    if (!_parameter) {
        _parameter=[NSMutableDictionary dictionaryWithDictionary:@{@"key":@"value"}];
    }
    return _parameter;
}

-(UISearchBar *)contentSearchBar{
    
    if (!_contentSearchBar) {
<<<<<<< HEAD
        _contentSearchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, kWidth, 35)];
=======
        _contentSearchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(80, 64, kWidth-80, 50)];
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
        _contentSearchBar.placeholder=@"请输入要搜索的内容";
        _contentSearchBar.delegate=self;
        
    }
    return _contentSearchBar;
}


-(UICollectionView *)spellCollectionView{
    
    if (!_spellCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 10;
//        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        //        layout.itemSize = CGSizeMake(kWidth-20, (kHeight-30)/3);
<<<<<<< HEAD
        _spellCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight - 64) collectionViewLayout:layout];
=======
        _spellCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight - 104) collectionViewLayout:layout];
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
        _spellCollectionView.backgroundColor=[UIColor whiteColor];
        _spellCollectionView.delegate=self;
        _spellCollectionView.dataSource=self;
    }
    return _spellCollectionView;
}

-(NSMutableArray *)titleArr{
    
    if (!_titleArr) {
        _titleArr = [NSMutableArray arrayWithArray:@[@"综合",@"人气",@"销量",@"价格"]];
    }
    return _titleArr;
}


-(NSMutableArray *)secondTitleArr{
    
    if (!_secondTitleArr) {
        _secondTitleArr = [NSMutableArray arrayWithArray:@[@"全部",@"服装城",@"零食馆",@"全球购",@"美妆馆",@"数码馆",@"百货馆",@"蔬菜区",@"玩具馆",@"鲜果区",@"超市",@"水果区",@"海鲜",@"1"]];
    }
    return _secondTitleArr;
}

-(NSMutableArray *)cellArr{
    
    if (!_cellArr) {
<<<<<<< HEAD
        _cellArr = [NSMutableArray arrayWithArray:@[@"SearchCell",@"FirstClassificationCell",@"SecondClassificationCell",@"FourthSectionCell"]];
=======
        _cellArr = [NSMutableArray arrayWithArray:@[@"SearchCell",@"FirstClassificationCell",@"SecondClassificationCell",@"FourthSectionCell",]];
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
    }
    return _cellArr;
}

-(NSArray *)id_cellArray{
    
    if (!_id_cellArray) {
        _id_cellArray = @[@"searchCell",@"firstCell",@"secondCell",@"fourthCell"];
    }
    return _id_cellArray;
}

#pragma mark － 自定义
<<<<<<< HEAD
// 选中Label改变背景色
- (void)selBackgroundColorLabel:(UILabel *)label
{
    [self.selectedLabel setBackgroundColor:[UIColor whiteColor]];
    //    self.selLabel.transform = CGAffineTransformIdentity;
    [self.selectedLabel setTextColor:[UIColor blackColor]];
    [label setTextColor:[UIColor redColor]];
    self.selectedLabel = label;
}

- (void)selBackgroundColor:(UILabel *)label
{
    [self.selLabel setBackgroundColor:[UIColor whiteColor]];
    //    self.selLabel.transform = CGAffineTransformIdentity;
    [self.selLabel setTextColor:[UIColor blackColor]];
    [label setBackgroundColor:[UIColor redColor]];
    [label setTextColor:[UIColor whiteColor]];
    self.selLabel = label;
}

=======
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d

#pragma mark － UISearchBarDelegate


#pragma mark － UICollectionViewDelegate,UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    
    return 4;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 1;
    }else if (section == 1){
        
        return self.titleArr.count;
    }else if (section == 2){
        return self.secondTitleArr.count;
    }else {
        return 5;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //    BaseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.id_cellArray[indexPath.section] forIndexPath:indexPath]
    if (indexPath.section == 0) {
        SearchCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"searchCell" forIndexPath:indexPath];
        cell.contentSearch.placeholder = @"请输入要搜索的内容";
        return cell;
    }else if (indexPath.section == 1){
        
        FirstClassificationCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"firstCell" forIndexPath:indexPath];
<<<<<<< HEAD
        if (indexPath.row == 0) {
            
            [self selBackgroundColor:cell.titleLabel];
        }
        cell.titleLabel.text = self.titleArr[indexPath.row];
        cell.layer.cornerRadius = 10;
        cell.layer.masksToBounds = YES;
        cell.tag = 1000 + indexPath.row;
=======
        cell.titleLabel.text = self.titleArr[indexPath.row];
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
        return cell;
    }else if (indexPath.section ==2){
        SecondClassificationCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"secondCell" forIndexPath:indexPath];
        cell.titleLabel.text = self.secondTitleArr[indexPath.row];
<<<<<<< HEAD
        if (indexPath.row == 0) {
            [self selBackgroundColorLabel:cell.titleLabel];
        }
        cell.layer.cornerRadius = 10;
        cell.layer.masksToBounds = YES;
        cell.tag = 2000 + indexPath.row;
=======
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
        return cell;
    }else{
        FourthSectionCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"fourthCell" forIndexPath:indexPath];
        return cell;
    }
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
<<<<<<< HEAD
   
     if (indexPath.section == 1){
        
        FirstClassificationCell *cell=[collectionView viewWithTag:1000 + indexPath.row];
        [self selBackgroundColor:cell.titleLabel];
    }else if (indexPath.section == 2){
        
        SecondClassificationCell *cell = [collectionView viewWithTag:2000 + indexPath.row];
        [self selBackgroundColorLabel:cell.titleLabel];
    }
=======
    
    NSLog(@"%ld",(long)indexPath.row);
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
<<<<<<< HEAD
        return CGSizeMake(kWidth, 35);
=======
        return CGSizeMake(kWidth, 45);
>>>>>>> 5bee59541ff07dbcbaa6fe419e6df24b39c5e23d
    }else if (indexPath.section == 1){
        return CGSizeMake(kWidth / 5, kHeight / 20);
    }else if (indexPath.section == 2){
        return CGSizeMake(kWidth / 6, kHeight / 20);
    }else{
        return CGSizeMake((kWidth- 30) / 2, kHeight / 3);
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    if (section == 1) {
        return UIEdgeInsetsMake(10, 10, 10, 10);
    }else{
        return UIEdgeInsetsMake(10, 10, 10, 10);
    }
}

#pragma mark － 视图的生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.contentSearchBar];
    [self.view addSubview:self.spellCollectionView];
    
    for (int i = 0; i < self.id_cellArray.count; i++) {
        [self.spellCollectionView registerNib:[UINib nibWithNibName:self.cellArr[i] bundle:nil] forCellWithReuseIdentifier:self.id_cellArray[i]];
    }
     // Do any additional setup after loading the view from its nib.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
@end
